<?php
class Shopping_Model_Item extends Zend_Db_Table{
	protected $_name = 'products';
	protected $_primary = 'id';
	
	protected $_ids;
	
	public function getItem($arrParam, $options = null){
		
		if($options['task'] == 'public-detail'){
			$select = $this->select()
						   ->where('id = ?',$arrParam['id'], 'INTEGER');
			$result = $this->fetchRow($select)->toArray();
		}
		
		return $result;
	}
	public function countItem($arrParam, $options = null){
		
		if($options['task'] == 'public-index'){
			$db = Zend_Registry::get('connectDb');
			$select = $db->select()
						 ->from('products as p',array('COUNT(p.id) AS totalItem'))
						 ->where('p.status = 1');
			$result = $db->fetchOne($select);
		}
		
		if($options['task'] == 'public-category'){
			$db = Zend_Registry::get('connectDb');
			$select = $db->select()
						 ->from('products as p',array('COUNT(p.id) AS totalItem'))
						 ->where('p.status = 1')
						  ->where('p.cat_id IN (' . $this->_ids . ')');
			$result = $db->fetchOne($select);
		}
		return $result;					 
	}
	
	public function listItem($arrParam, $options = null){
		
		if($options['task'] == 'view-cart'){
			/*echo '<pre>';
			print_r($arrParam);
			echo '</pre>';*/
			if(count($arrParam['cart'])>0){
				$i = 1;
				foreach ($arrParam['cart'] as $key => $val){
					if($i  == 1 ){
						$ids .=  $key;
					}else{
						$ids .=  ',' . $key;
					}
					$i ++;
				}
				//echo $ids;
				
				$db = Zend_Registry::get('connectDb');
				$select = $db->select()
							 ->from('products as p',array('id','name','price','selloff','cat_id'))
							 ->join('product_category as pc','pc.id = p.cat_id',array('name as category_name'))
							 ->where('p.status = 1')
							 ->where('p.id IN (' . $ids . ')');
									 
				$result = $db->fetchAll($select);
			}
		}
		if($options['task'] == 'public-index'){
			
			
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($abc,$abc);
			$paginator  = $arrParam['paginator'];
			/*echo '<pre>';
			print_r($paginator);
			echo '</pre>';*/
			$select = $db->select()
						 ->from('products as p',array('id','name','picture','price','selloff','cat_id'))
						 ->join('product_category as pc','pc.id = p.cat_id',array('name as category_name'))
						 ->where('p.status = 1')
						 ->order(' id DESC')
						 ->limitPage($paginator['currentPage'],$paginator['itemCountPerPage']);
			$result = $db->fetchAll($select);
		}
		
		if($options['task'] == 'public-category'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($abc,$abc);
			$select = $db->select()
						 ->from('product_category', array('id','name','parents'))
						 ->where('status = 1');
			$resultCategory = $db->fetchAll($select);		
			$system = new Zendvn_System_Recursive($resultCategory);
			$newArray = $system->buildArray($arrParam['cid']);
			
			$tmp[] = $arrParam['cid'];	
			if(!empty($newArray)){
				foreach ($newArray as $key => $val){
					$tmp[] = $val['id'];
				}	 
			}
			
			
			$ids = implode(',',$tmp);
			$this->_ids = $ids;
			$paginator  = $arrParam['paginator'];
			$select = $db->select()
						 ->from('products as p',array('id','name','picture','price','selloff','cat_id'))
						 ->join('product_category as pc','pc.id = p.cat_id',array('name as category_name'))
						 ->where('p.status = 1')
						 ->where('p.cat_id IN (' . $ids . ')')
						 ->order(' id DESC')
						 ->limitPage($paginator['currentPage'],$paginator['itemCountPerPage']);
			//echo '<br>' . $select;							 
			$result = $db->fetchAll($select);
		}
		
		return $result;
	}	
}